<template>
  <div
    class="site-wrapper"
    :class="{ 'site-sidebar--fold': sidebarFold }"
    v-loading.fullscreen.lock="loading"
    element-loading-text="拼命加载中">
    <template v-if="!loading">
      <main-navbar />
      <main-sidebar />
      <div class="site-content__wrapper" :style="{ 'min-height': documentClientHeight + 'px' }">
        <main-content />
      </div>
    </template>
  </div>
</template>

<script>
  import MainNavbar from './main-navbar'
  import MainSidebar from './main-sidebar'
  import MainContent from './main-content'
  import ar from 'element-ui/src/locale/lang/ar'
  export default {
    data () {
      return {
        loading: true
      }
    },
    components: {
      MainNavbar,
      MainSidebar,
      MainContent
    },
    computed: {
      documentClientHeight: {
        get () { return this.$store.state.common.documentClientHeight },
        set (val) { this.$store.commit('common/updateDocumentClientHeight', val) }
      },
      sidebarFold: {
        get () { return this.$store.state.common.sidebarFold }
      },
      userId: {
        get () { return this.$store.state.user.id },
        set (val) { this.$store.commit('user/updateId', val) }
      },
      userName: {
        get () { return this.$store.state.user.name },
        set (val) { this.$store.commit('user/updateName', val) }
      },
      areaAllInfo: {
        get () { return this.$store.state.user.area },
        set (val) { this.$store.commit('user/areaList', val) }
      },
      targetInfo: {
        get () { return this.$store.state.user.target },
        set (val) { this.$store.commit('user/targetList', val) }
      }
    },
    created () {
      this.getUserInfo()
    },
    mounted () {
      this.resetDocumentClientHeight()
    },
    methods: {
      // 重置窗口可视高度
      resetDocumentClientHeight () {
        this.documentClientHeight = document.documentElement['clientHeight']
        window.onresize = () => {
          this.documentClientHeight = document.documentElement['clientHeight']
        }
      },
      // 获取当前管理员信息
      getUserInfo () {
        this.$http({
          url: this.$http.adornUrl('/sys/user/info'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          this.loading = false
          this.userId = data.userId
          this.userName = data.username
          let areaAllInfo = {
            province: [],
            city: [],
            area: []
          };
          data.areaAllInfoList.forEach((item) => {
            let area = item.split("-");
            if(area[0] === "1"){
              areaAllInfo.province.push({
                parent : area[3],
                code : area[1],
                name : area[2]
              })
            }
            if(area[0] === "2"){
              areaAllInfo.city.push({
                parent : area[3],
                code : area[1],
                name : area[2]
              })
            }
            if(area[0] === "3"){
              areaAllInfo.area.push({
                parent : area[3],
                code : area[1],
                name : area[2]
              })
            }
          })
          this.areaAllInfo = areaAllInfo
          this.targetInfo = data.basIndexList;
          // console.log('🚀 ~ this.$store.state.user.area', this.$store.state.user.area)
          // console.log('🚀 ~ this.$store.state.user.target', this.$store.state.user.target)
        })

      }
    }
  }
</script>
